<?php
class Application_Model_User extends Zend_Db_Table_Abstract
{
    const USER_NAMESPACE = 'zs_User_2pretty';
    protected $_session;
    protected $_name = 'users';
    protected $_primary = 'user_id';

    protected function _loadByUserName($email)
    {
        $select = $this->getAdapter()->select();
        $select->from($this->_name);
        $select->joinLeft('user_detail', 'users.user_id = user_detail.user_id');
        $select->where('email=?', $email);
        $select->where('provider=?', 'web');
        $result = $this->getAdapter()->fetchRow($select);
        
        return $result;
    }

    public function _loadByUserNameProvider($email, $provider)
    {
        $select = $this->getAdapter()->select();
        $select->from($this->_name);
        $select->where('email=?', $email);
        $select->where('provider=?', $provider);
//echo $select->__toString();exit;
        $result = $this->getAdapter()->fetchRow($select);
        
        return $result;
    }
    
    protected function _validatePassword($password, $userpass)
    {
        if ($this->_hashPassword($password) == $userpass) {
            return true;
        }
        return false;
    }

    protected function _hashPassword($password)
    {
        return md5($password);
    }

    public function getSession()
    {
        if (empty($this->_session)) {
            $this->_session = new Zend_Session_Namespace(self::USER_NAMESPACE);
            $this->_session->setExpirationSeconds(450);
        }
        return $this->_session;
    }

    public function isLoggedIn()
    {
        if (isset($this->getSession()->isLoggedIn)) {
            return $this->getSession()->isLoggedIn;
        }
        return false;
    }

    public function setIsLoggedIn($isLoggedIn = false)
    {
        $this->getSession()->isLoggedIn = $isLoggedIn;
        return $this;
    }

    public function login($email, $password)
    {
        $row = $this->_loadByUserName($email);
        if ($row['user_id'] && $this->_validatePassword($password, $row['password'])) {
            /* login success */
            $this->setIsLoggedIn(true);
            $this->getSession()->userId = $row['user_id'];
            $this->getSession()->email = $row['email'];
            $this->getSession()->firstName = $row['first_name'];
            $this->getSession()->lastName = $row['last_name'];
        } else {
            /* login fail */
            $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
            $flashMessenger->setNamespace('error')->addMessage('Invalid Username or Password');
        }
    }
    
    public function loginProvider($email, $provider)
    {
        $row = $this->_loadByUserNameProvider($email, $provider);
        if ($row['user_id']) {
            /* login success */
            $this->setIsLoggedIn(true);
            $this->getSession()->userId = $row['user_id'];
            $this->getSession()->email = $row['email'];
            $this->getSession()->provider = $row['provider'];
            $this->getSession()->provider_user_id = $row['provider_user_id'];
        }
    }
    
    public function getDetailUser($id)
    {
        $select = $this->getAdapter()->select();
        $select->from($this->_name);
        $select->joinLeft('user_detail', 'users.user_id = user_detail.user_id');
        $select->where('users.user_id=?', $id);
        $result = $this->getAdapter()->fetchRow($select);
        
        return $result;
    }
    
    public function _existsUser($email)
    {
        $select = $this->getAdapter()->select();
        $select->from($this->_name);
        $select->joinLeft('user_detail', 'users.user_id = user_detail.user_id');
        $select->where('email=?', $email);
        $result = $this->getAdapter()->fetchRow($select);
        
        return $result;
    }
}